// BattLineSvc
// Copyright (C) 2009, Avian Waves Software
// SEE LICENSE.TXT for software licensing information

// *************************************************************************************************************
// Note: This app is usually not up-to-date.  It's just a test EXE for the service when working on new features.
// *************************************************************************************************************

#include "stdafx.h"
#include "BattLineEXE.h"


#define MAX_LOADSTRING 100

// Global Variables:
HWND g_hTaskBar = NULL;
HWND g_hWnd = NULL;
int g_width = -1;
int g_height = 3;

// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE, LPTSTR);
BOOL InitInstance(HINSTANCE);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
int BattLineMain(HINSTANCE hInstance);
void DrawWindow();

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
  return BattLineMain(hInstance);
}

int BattLineMain(HINSTANCE hInstance)
{
  MSG msg;

  // Perform application initialization:
  if (!InitInstance(hInstance)) 
  {
    return FALSE;
  }

  // Main message loop:
  while (GetMessage(&msg, NULL, 0, 0)) 
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }

  return (int) msg.wParam;
}

ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
  WNDCLASS wc;

  wc.style         = CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc   = WndProc;
  wc.cbClsExtra    = 0;
  wc.cbWndExtra    = 0;
  wc.hInstance     = hInstance;
  wc.hIcon         = 0;
  wc.hCursor       = 0;
  wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  wc.lpszMenuName  = 0;
  wc.lpszClassName = szWindowClass;

  return RegisterClass(&wc);
}

BOOL InitInstance(HINSTANCE hInstance)
{
  TCHAR szTitle[MAX_LOADSTRING] = _T("");		// title bar text
  TCHAR szWindowClass[MAX_LOADSTRING] = _T("BattLineSvcClass");	// main window class name

  // SHInitExtraControls should be called once during your application's initialization to initialize any
  // of the device specific controls such as CAPEDIT and SIPPREF.
  SHInitExtraControls();

  if (!MyRegisterClass(hInstance, szWindowClass))
  {
    return FALSE;
  }

  g_width = GetSystemMetrics(SM_CXSCREEN);
  //g_height = GetSystemMetrics(SM_CYSCREEN);

  g_hTaskBar = FindWindow(_T("HHTaskBar"), NULL);

  if (!g_hTaskBar)
  {
    return FALSE;
  }

  g_hWnd = CreateWindowEx(WS_EX_TOPMOST, szWindowClass, szTitle, WS_CHILD | WS_VISIBLE, 0, 0, g_width, g_height, g_hTaskBar, NULL, hInstance, NULL);

  if (!g_hWnd)
  {
    return FALSE;
  }

  ShowWindow(g_hWnd, SW_SHOWNORMAL);
  UpdateWindow(g_hWnd);

  return TRUE;
}

void DrawWindow()
{
  PAINTSTRUCT ps;
  HDC hdc;
  HBRUSH sb;
  RECT rc;

  int newWidth = GetSystemMetrics(SM_CXSCREEN); // Get new screen dimensions, to see if it changed
  
  if (newWidth != g_width)
  {
    g_width = newWidth;
    MoveWindow(g_hWnd, 0, 0, g_width, g_height, TRUE);
  }
  else
  {
    hdc = BeginPaint(g_hWnd, &ps);

    // Paint background
    sb = CreateSolidBrush(0);
    rc.left = 0; rc.top = 0; rc.right = g_width; rc.bottom = g_height;
    FillRect(hdc, &rc, sb);
    DeleteObject(sb);

    // Paint levels
    sb = CreateSolidBrush(RGB(0,255,0));
    rc.left = 0; rc.top = 0; rc.right = (int)(g_width * 0.7); rc.bottom = g_height;
    FillRect(hdc, &rc, sb);
    DeleteObject(sb);

    EndPaint(g_hWnd, &ps);
  }
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  static SHACTIVATEINFO s_sai;

  switch (message) 
  {
  case WM_LBUTTONDOWN:
  case WM_LBUTTONUP:
  case WM_RBUTTONDOWN:
  case WM_RBUTTONUP:
  case WM_MOUSEMOVE:
    // This passes any touch screen activity up to the actual task bar
    PostMessage(g_hTaskBar, message, wParam, lParam);
    break;

  case WM_PAINT:
    DrawWindow();
    break;

  case WM_DESTROY:
    PostQuitMessage(0);
    break;

  case WM_ACTIVATE:
    SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
    break;

  case WM_SETTINGCHANGE:
    SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
    break;

  default:
    return DefWindowProc(hWnd, message, wParam, lParam);
  }

  return 0;
}

